package com.common.sevice.impl;

import com.common.constanct.CommonUtils;
import com.common.entity.SystemApplicationWebEntity;
import com.common.model.SystemApplicationServiceModel;
import com.common.model.SystemApplicationWebModel;
import com.common.model.TreeModel;
import com.common.mysql.Where;
import com.common.service.impl.TreeSimpleServiceImpl;
import com.common.sevice.SystemApplicationWebService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Date: 2025/6/24 10:27
 */
@Service
public class SystemApplicationWebServiceImpl extends TreeSimpleServiceImpl<String, SystemApplicationWebModel, SystemApplicationWebEntity> implements SystemApplicationWebService {


    @Override
    public List<SystemApplicationWebModel> list(String appId) {
        List<SystemApplicationWebModel> applicationWebModelList = search(Where.build()
                .field("app_id").equal(appId)
                .and()
                .field("del_flag").equal(0));
        return buildTree(applicationWebModelList);
    }

    @Override
    public List<SystemApplicationWebModel> buildTree(List<SystemApplicationWebModel> modelList) {
        //获取所有parentId为空的集合，所有顶级节点
        List<SystemApplicationWebModel> topList = modelList.stream().filter(m -> ObjectUtils.isEmpty(m.getParentId()))
                .sorted(Comparator.comparingInt(SystemApplicationWebModel::getSort))
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(topList)) {
            return Collections.emptyList();
        }
        Map<String, List<SystemApplicationWebModel>> childrenMap = modelList.stream()
                .filter(m -> !ObjectUtils.isEmpty(m.getParentId()))
                .sorted(Comparator.comparingInt(SystemApplicationWebModel::getSort))
                .collect(Collectors.groupingBy(TreeModel::getParentId));
        build(topList, childrenMap);
        return topList;
    }

    @Override
    public void copy(String appId, String id) {
        List<SystemApplicationWebModel> applicationWebModelList = search(Where.build().field("app_id").equal(appId)
                .and().field("del_flag").equal(0));
        Map<String, List<SystemApplicationWebModel>> childrenMap = applicationWebModelList.stream()
                .filter(m -> StringUtils.hasText(m.getParentId()))
                .collect(Collectors.groupingBy(SystemApplicationWebModel::getParentId));
        Optional<SystemApplicationWebModel> topSystemApplicationWebModelOptional = applicationWebModelList.stream()
                .filter(m -> id.equals(m.getId()))
                .findFirst();
        if (!topSystemApplicationWebModelOptional.isPresent()) {
            return;
        }
        SystemApplicationWebModel systemApplicationWebModel = topSystemApplicationWebModelOptional.get();
        List<SystemApplicationWebModel> copyList = new LinkedList<>();
        findChildren(copyList, systemApplicationWebModel, childrenMap);
        if (!CollectionUtils.isEmpty(copyList)) {
            for (SystemApplicationWebModel applicationWebModel : copyList) {
                applicationWebModel.setId(CommonUtils.getUUID());
                long currentTimeMillis = System.currentTimeMillis();
                applicationWebModel.setCode(applicationWebModel.getCode() + currentTimeMillis);
                applicationWebModel.setLabel(applicationWebModel.getLabel() + currentTimeMillis);
                add(applicationWebModel);
            }
        }

    }

    @Override
    public void deleteChildren(String id) {
        List<SystemApplicationWebModel> applicationServiceModelList = searchAll();
        Map<String, List<SystemApplicationWebModel>> parentIdMap = applicationServiceModelList.stream()
                .filter(systemApplicationServiceModel -> !ObjectUtils.isEmpty(systemApplicationServiceModel.getParentId()))
                .collect(Collectors.groupingBy(SystemApplicationWebModel::getParentId));
        List<String> deleteIdList = new LinkedList<>();
        getDeleteIdList(parentIdMap, id, deleteIdList);
        deleteIdList.forEach(this::delete);
        delete(id);
    }

    private void getDeleteIdList(Map<String, List<SystemApplicationWebModel>> parentIdMap, String id, List<String> deleteIdList) {
        if (parentIdMap.containsKey(id)) {
            List<SystemApplicationWebModel> childrenList = parentIdMap.get(id);
            for (SystemApplicationWebModel model : childrenList) {
                deleteIdList.add(model.getId());
                getDeleteIdList(parentIdMap, model.getId(), deleteIdList);
            }
        }
    }

    private void findChildren(List<SystemApplicationWebModel> copyList, SystemApplicationWebModel systemApplicationWebModel, Map<String, List<SystemApplicationWebModel>> childrenMap) {
        String id = systemApplicationWebModel.getId();
        if (!childrenMap.containsKey(id)) {
            return;
        }
        List<SystemApplicationWebModel> childrenList = childrenMap.get(id);
        if (!CollectionUtils.isEmpty(childrenList)) {
            copyList.addAll(childrenList);
            for (SystemApplicationWebModel applicationWebModel : childrenList) {
                findChildren(copyList, applicationWebModel, childrenMap);
            }
        }
    }
}
